import { defineStore } from "pinia";

interface TabsObj {
    name: string;
    path: string;

}

interface TabsState {
    label: Array<TabsObj>;
    index: number;
    itemPath: string | number,
    clickName: string
}

export const useTabsStore = defineStore('tabs', {
    state: (): TabsState => ({
        label: [],
        index: 0,
        // tab选中高亮
        itemPath: '',
        clickName: ''
    }),
    actions: {
        addTab(obj: TabsObj) {
            // tab选中高亮
            this.itemPath = obj.path
            for (let i = 0; i < this.label.length; i++) {
                if (this.label[i].name === obj.name) return
            }
            this.label.push(obj)

        },
        deleteTab(path: string | number) {
            let indexObj = this.label.filter(item => item.path === path)
            this.index = this.label.findIndex(item => item === indexObj[0])
            this.label = this.label.filter(item => item.path !== path)
            // tab选中高亮
            this.itemPath = this.label[this.index - 1].path
        }
    },
    persist: {
        enabled: true,
        strategies: [{
            key: 'pinia-pressure-tabs',
            storage: localStorage,
            paths: ['label', 'itemPath', 'index']
        }]
    },
})
